1493D - GCD of an Array - CodeForces Solution


brute force data structures hashing implementation math number theory sortings two pointers *2100

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+5;
const long long mod=1e9+7;
struct segmenttree
{
	vector<int> seg;
	void update(int l,int r,int i,int val,int pos)
	{
		if(i<l||r<i) return ;
		if(l==r) { seg[pos]+=val;return ; }
		int mid=(l+r)/2;
		update(l,mid,i,val,pos*2);
		update(mid+1,r,i,val,pos*2+1);
		seg[pos]=min(seg[pos*2],seg[pos*2+1]);
	}
};
segmenttree mp[MAXN];
map<int,int> cnt[MAXN];
int pre[MAXN],pos[MAXN*2],val[MAXN*2],ct[MAXN];
int main()
{
//    ios::sync_with_stdio(0);
//    cin.tie(0);
//    cout.tie(0);
    for(int i=1;i<MAXN;i++) pre[i]=1e9;
    for(int i=2;i<MAXN;i++) for(int j=i;j<MAXN;j+=i) pre[j]=min(pre[j],i);
    int n,q;
    cin>>n>>q;
    long long ans=1;
    for(int i=1;i<=n+q;i++)
    {
    	if(i<=n)
    	{
    		pos[i]=i;
    		cin>>val[i];
    		int r=val[i];
    		while(r>1) ct[pre[r]]+=(cnt[pre[r]][i]++==0),r/=pre[r];
		}
		else
		{
    		cin>>pos[i]>>val[i];
    		int r=val[i];
    		while(r>1) ct[pre[r]]+=(cnt[pre[r]][pos[i]]++==0),r/=pre[r];
		}
	}
	for(int i=1;i<MAXN;i++) if(ct[i]==n) mp[i].seg.resize(n*4+5);
	for(int i=1;i<=n+q;i++)
	{
		int r=val[i];
    	while(r>1)
    	{
    		if(ct[pre[r]]!=n) { r/=pre[r];continue; }
    		int a=mp[pre[r]].seg[1];
    		mp[pre[r]].update(1,n,pos[i],1,1);
    		if(mp[pre[r]].seg[1]-a) ans=ans*pre[r]%mod;
			r/=pre[r];
		}
		if(i>n) cout<<ans<<"\n";
	}
}


Comments

Submit
0 Comments
More Questions

1566B - MIN-MEX Cut
678C - Joty and Chocolate
1352E - Special Elements
1520E - Arranging The Sheep
1157E - Minimum Array
1661D - Progressions Covering
262A - Roma and Lucky Numbers
1634B - Fortune Telling
1358A - Park Lighting
253C - Text Editor
365B - The Fibonacci Segment
75A - Life Without Zeros
1519A - Red and Blue Beans
466A - Cheap Travel
659E - New Reform
1385B - Restore the Permutation by Merger
706A - Beru-taxi
686A - Free Ice Cream
1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs